package com.haikesi.api.deviceposition.service.impl;

import com.haikesi.api.base.Error;
import com.haikesi.api.base.*;
import com.haikesi.api.deviceposition.dao.DevicePositionDao;
import com.haikesi.api.deviceposition.domain.po.DevicePositionPO;
import com.haikesi.api.deviceposition.domain.vo.*;
import com.haikesi.api.deviceposition.service.IDevicePositionService;
import com.haikesi.api.typetemplate.dao.TypeTemplateMapper;
import com.haikesi.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: DevicePositionServiceImpl
 * @program: haikesi-platform-apis
 * @Author: TanJun
 * @Date: 2019/5/14 15:06
 * @Description: 位置业务逻辑实现层
 * @Version: 1.0
 * @Copyright: 2019 重庆海克斯科技有限公司. 注意：本内容仅限于重庆海克斯科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */

@Service
@Transactional
public class DevicePositionServiceImpl implements IDevicePositionService {

    @Autowired
    private DevicePositionDao dao;

    private TypeTemplateMapper typeTemplateDao;

    @Override
    public String create(DevicePositionSaveVO seveVO) {
        /*DevicePositionPO po = new DevicePositionPO();
        User user = po.getUser();
        if(seveVO.getIsScreenOrganization().equals("1") && user.getOrganizationId() != null ){
            seveVO.setOrganizationId(user.getOrganizationId());
        }else {
            seveVO.setOrganizationId("0");
        }*/

        DevicePositionPO po = new DevicePositionPO();
        User user = po.getUser();
        if (user !=null){
            seveVO.setOrganizationId(user.getOrganizationId());
        }

        BeanUtils.copyProperties(seveVO, po);
        //需要补充 上级位置ID集合(用逗号隔开所有上级ID) 和 上级位置名字集合
        if(StringUtils.isNotBlank(po.getParentId())&&!po.getParentId().equals("0")){
            DevicePositionPO devicePositionPO = dao.selectByPrimaryKey(po.getParentId());
            if (devicePositionPO==null) {
                throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("parentId", "父类不存在")));
            }
            po.setParentIds(devicePositionPO.getId());
            po.setParentName(devicePositionPO.getDevicePositionName());
            if(!devicePositionPO.getParentId().equals("0")){
                po.setParentIds(devicePositionPO.getParentIds()+","+devicePositionPO.getId());
                po.setParentName(devicePositionPO.getParentName()+","+devicePositionPO.getDevicePositionName());
            }
        }
        po.preInsert();
        int i = dao.insertSelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        return po.getId();
    }

    @Override
    public void update(DevicePositionUpdateVO updateVO) {
        DevicePositionPO po = new DevicePositionPO();
        BeanUtils.copyProperties(updateVO, po);
        //需要补充 父级机构ID集合(用逗号隔开所有父级ID) 和 父级机构名字集合
        if(StringUtils.isNotBlank(po.getParentId())){
            DevicePositionPO devicePositionPO = dao.selectByPrimaryKey(po.getParentId());
            if (devicePositionPO==null) {
                throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("parentId", "父类不存在")));
            }
            po.setParentIds(devicePositionPO.getId());
            po.setParentName(devicePositionPO.getDevicePositionName());
            if(!devicePositionPO.getParentId().equals("0")){
                po.setParentIds(devicePositionPO.getParentIds()+","+devicePositionPO.getId());
                po.setParentName(devicePositionPO.getParentName()+","+devicePositionPO.getDevicePositionName());
            }
        }
        po.preUpdate();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }

    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.FORMAT_EXCEPTION);
        }
        DevicePositionPO devicePositionPO = dao.selectByPrimaryKey(id);
        if (devicePositionPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("id", "机构不存在")));
        }
        DevicePositionPO po =new DevicePositionPO();
        po.setId(id);
        po.preDelete();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        //删除下级的机构
        dao.deleteByParentId(id);
    }

    @Override
    public List<DevicePositionVO> getList(DevicePositionSearchVO searchVO) {
        return dao.getList(searchVO);
    }

    @Override
    public List<DevicePositionTreeVO> getTreeList(DevicePositionTreeSearchVO searchVO) {
        List<DevicePositionTreeVO> retunList=new ArrayList<>();

        /*逻辑判断
        * 1：传入组织id并且为管理员 查询所有数据
        * 2: 传入组织id并且为普通用户 查询对应的数据
        * 3：不传入组织id查询组织id 查询组织id为0的数据*/
        /*DevicePositionPO po = new DevicePositionPO();
        if(po.getUser()==null){
            searchVO.setIsScreenOrganization("0");
            searchVO.setOrganizationId("0");
        }
        //设置组织id
        searchVO.setOrganizationId(po.getUser().getOrganizationId());
        if(StringUtils.isBlank(searchVO.getIsScreenOrganization())|| searchVO.getIsScreenOrganization().equals("0")){
            searchVO.setIsScreenOrganization("0");
            searchVO.setOrganizationId("0");
        }
        if(po.getUser().getIsAdmin()==1){
            searchVO.setOrganizationId("");
        }*/

        DevicePositionPO po = new DevicePositionPO();
        User user = po.getUser();
        if(user !=null && user.getIsAdmin()!=1){
            searchVO.setOrganizationId(user.getOrganizationId());
        }
        if(user !=null && user.getIsAdmin()==1){
            searchVO.setOrganizationId("");
        }

        List<DevicePositionTreeVO> list=dao.getTreeList(searchVO);
        HashMap<String,DevicePositionTreeVO> map=new HashMap<>();
        for (DevicePositionTreeVO vo:list) {
            map.put(vo.getId(),vo);
        }
        for (DevicePositionTreeVO vo:list) {
            if(vo.getParentId().equals("0")){
                retunList.add(vo);
            }else {
                DevicePositionTreeVO parentVO=map.get(vo.getParentId());
                if(parentVO.getChildrens()==null){
                    parentVO.setChildrens(new ArrayList<>());
                }
                parentVO.getChildrens().add(vo);
            }
        }
        return retunList;
    }
}
